/**
 * 
 */
package dms.service.imp;

import java.util.Date;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import com.puppycrawl.tools.checkstyle.api.Context;

import dms.model.Building;
import dms.model.Dormitory;
import dms.model.exception.NoSuchEntityException;
import dms.model.form.Mail;
import dms.model.role.Student;
import dms.model.util.PageIterator;
import dms.repository.MailRepository;
import dms.service.DormitoryService;
import dms.service.MailQueryService;
import dms.util.ContextUtil;

/**
 * @author 李健
 *
 */
public class MailQueryServiceImpl implements MailQueryService {
	
	private static final Log log = LogFactory.getLog(MailQueryServiceImpl.class);

	private MailRepository mailRepository = null;
	
	/**
	 * @return the mailRepository
	 */
	public MailRepository getMailRepository() {
		return mailRepository;
	}

	/**
	 * @param mailRepository the mailRepository to set
	 */
	public void setMailRepository(MailRepository mailRepository) {
		this.mailRepository = mailRepository;
	}

	@Override
	public PageIterator<Mail> findByBuilding(int start, int count,
			Building building, Date fromDate, Date toDate) {
		return this.mailRepository.findByBuilding(start, count, building, fromDate, toDate);
	}

	@Override
	public Mail findById(Long id) {
		return this.mailRepository.findById(id);
	}


	/**
	 * @param mailRepository
	 */
	public MailQueryServiceImpl(MailRepository mailRepository) {
		super();
		this.mailRepository = mailRepository;
	}

	@Override
	public PageIterator<Mail> findByBuildingAndStudent(int start, int count,
			Building building, String student, Date fromDate, Date toDate) {
		
		return this.mailRepository.findByBuildingStudent(start, count, building, student, fromDate, toDate);
	}
	
	@Override
	public PageIterator<Mail> findByDormitory(int start, int count, Dormitory dormitory, Date fromDate, Date toDate) {
		if (dormitory == null) {
			throw new NullPointerException("dormitory为空");
		}
		return this.mailRepository.findByDormitory(start, count, dormitory, fromDate, toDate);
	}


	@Override
	public PageIterator<Mail> findByStudent(int start, int count,
			Student student, Date fromDate, Date toDate) {
		return this.mailRepository.findByStudent(start, count, student.getAccount().getName(), fromDate, toDate);
	}

	/**
	 * 
	 */
	public MailQueryServiceImpl() {
		super();
	}

	@Override
	public PageIterator<Mail> findByDormitory(int start, int count,
			long dormitoryId, Date fromDate, Date toDate) throws NoSuchEntityException {
		Dormitory dormitory = null;
		DormitoryService dormitoryService = (DormitoryService) ContextUtil.getContext().getBean("dormitoryService");
		dormitory = dormitoryService.findById(dormitoryId);
		
		if (dormitory == null) {
			throw new NoSuchEntityException("没有该寝室");
		}
		
		return this.findByDormitory(start, count, dormitory, fromDate, toDate);
	}

}
